<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use DB;
use Hash;

class LoginController extends Controller
{
    


    /**
     * 显示登陆的页面
     */
    public function login()
    {
        return view('admin.login.login');
    } 



    /**
     * 处理登陆的请求
     */
    public function dologin(Request $request)
    {
        //
        //验证表单
         $this->validate($request, [
            'username' => 'required|regex:/\w{8,30}/',
            'password' => 'required|regex:/\w{8,30}/',
         ],['username.required'=>'用户名不能省略',
            'username.regex'=>'用户名长度不符合要求，请填写8-25位数字或字母或汉字',
            'password.required'=>'密码不能为空',
            'password.regex'=>'密码长度不符合要求，请填写8-25位数字或字母',
         ]);
        //dd($request);
        //return view('admin.login.login');
        //实例化用户对象
        $user=User::where('username',$request->username)->firstOrFail();
         //获取用户信息
        if (Hash::check($request->password,$user->password)) {
            //写入登陆状态
            session(['uid'=>$user->id]);
            return redirect('/admin');
        }else{
             return back()->with('info','登录失败！');
        }

    } 


    /**
     * 登出
     */
    public function logout(Request $request)
    {
        $request->session()->flush();
        return redirect('/');
    }




    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
